package edu.zqu.course.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.zqu.course.project.domain.pojo.CourseSyllabusContent;
import edu.zqu.course.project.domain.pojo.CourseSyllabusDescription;
import edu.zqu.course.project.mapper.CourseSyllabusContentMapper;
import edu.zqu.course.project.mapper.CourseSyllabusDescriptionMapper;
import edu.zqu.course.project.service.CourseSyllabusDescriptionService;
import edu.zqu.course.response.SessionShow;
import edu.zqu.course.response.SessionShow01;
import edu.zqu.course.response.TeachingProgram;
import edu.zqu.course.response.TeachingProgramDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程大纲内容，即：章 服务实现类
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-06-24
 */
@Service
public class CourseSyllabusDescriptionServiceImpl extends ServiceImpl<CourseSyllabusDescriptionMapper, CourseSyllabusDescription> implements CourseSyllabusDescriptionService {
    @Resource
    CourseSyllabusDescriptionMapper courseSyllabusDescriptionMapper;

    @Resource
    CourseSyllabusContentMapper courseSyllabusContentMapper;

    @Override
    public List<CourseSyllabusDescription> getCourseSyllabusDescriptionList() {
        return courseSyllabusDescriptionMapper.selectList(null);
    }

    @Override
    public List<TeachingProgram> getCsdTpByCsIdAndChapter(String csId, Integer page, Integer size) {
        //设置查询条件
        QueryWrapper<CourseSyllabusDescription> wrapper = new QueryWrapper<>();
        wrapper.eq("course_syllabus_id", csId);
        //设置分页：第一个参数：页码，第二个参数：页面大小，即每页显示多少条数据
        Page page1 = new Page(page, size);
        List<CourseSyllabusDescription> courseSyllabusDescriptions = courseSyllabusDescriptionMapper
                .selectPage(page1, wrapper)
                .getRecords();
        //将查询出来的结果封装到TeachingProgram对象中
        List<TeachingProgram> teachingPrograms = new ArrayList<TeachingProgram>();
        for (CourseSyllabusDescription courseSyllabusDescription : courseSyllabusDescriptions) {
            String name = courseSyllabusDescription.getName();
            Integer teachingHour = courseSyllabusDescription.getTeachingHour();
            String content = courseSyllabusDescription.getContent();
            TeachingProgram teachingProgram = new TeachingProgram(name, teachingHour, content);
            teachingPrograms.add(teachingProgram);
        }
        //返回结果
        return teachingPrograms;
    }

    @Override
    public TeachingProgramDetail getCsdTpdById(Long id) {
        //通过id查询数据并封装到CourseSyllabusDescription对象中
        CourseSyllabusDescription courseSyllabusDescription = courseSyllabusDescriptionMapper.selectById(id);

        //封装到 TeachingProgramDetail 对象中
        Integer chapter = courseSyllabusDescription.getChapter();
        String name = courseSyllabusDescription.getName();
        Integer teachingHour = courseSyllabusDescription.getTeachingHour();
        String content = courseSyllabusDescription.getContent();
        String goal = courseSyllabusDescription.getGoal();
        String requirement = courseSyllabusDescription.getRequirement();
        TeachingProgramDetail teachingProgramDetail = new TeachingProgramDetail(
                chapter, name, teachingHour, content, goal,
                requirement);

        //返回结果
        return teachingProgramDetail;
    }

    @Override
    public SessionShow getSessionShowById(Long id) {
        //查询数据
        CourseSyllabusContent courseSyllabusContent = courseSyllabusContentMapper.selectById(id);
        Long CSDId = courseSyllabusContent.getCourseSyllabusDescriptionId();
        CourseSyllabusDescription courseSyllabusDescription = courseSyllabusDescriptionMapper.selectById(CSDId);

        //封装到 SessionShow 对象
        SessionShow01 sessionShow01 = new SessionShow01(courseSyllabusContent.getId(), courseSyllabusContent.getSession(),
                courseSyllabusContent.getName(), courseSyllabusContent.getTeachingHour(), courseSyllabusContent.getContent(),
                courseSyllabusContent.getImportant(), courseSyllabusContent.getDifficult());
        SessionShow sessionShow = new SessionShow(courseSyllabusDescription.getId(), courseSyllabusDescription.getChapter(),
                courseSyllabusDescription.getName(), courseSyllabusDescription.getTeachingHour(), sessionShow01,
                courseSyllabusDescription.getInstruction());

        //返回结果
        return sessionShow;
    }

    @Override
    public Integer deleteCourseSyllabusDescriptionList() {
        return courseSyllabusDescriptionMapper.delete(null);
    }

    @Override
    public Integer deleteByCSDID(Long id) {
        return courseSyllabusDescriptionMapper.deleteById(id);
    }

    @Override
    public Integer putCsdTpdByCsIdAndChapter(CourseSyllabusDescription courseSyllabusDescription) {
        Integer result = courseSyllabusDescriptionMapper.updateById(courseSyllabusDescription);
        return result;
    }

    @Override
    public Integer putSessionShow(SessionShow sessionShow) {
        //更新章
        CourseSyllabusContent courseSyllabusContent1 = courseSyllabusContentMapper
                .selectById(sessionShow.getSessionShow01().getId());
        Long courseSyllabusDescriptionId = courseSyllabusContent1.getCourseSyllabusDescriptionId();
        CourseSyllabusDescription courseSyllabusDescription = courseSyllabusDescriptionMapper
                .selectById(courseSyllabusDescriptionId);
        courseSyllabusDescription.setChapter(sessionShow.getChapter());
        courseSyllabusDescription.setName(sessionShow.getName());
        courseSyllabusDescription.setTeachingHour(sessionShow.getTeachingHour());
        courseSyllabusDescription.setInstruction((sessionShow.getInstruction()));
        int updateCSDResult = courseSyllabusDescriptionMapper.updateById(courseSyllabusDescription);

        //更新节
        SessionShow01 sessionShow01 = sessionShow.getSessionShow01();
        CourseSyllabusContent courseSyllabusContent = courseSyllabusContentMapper
                .selectById(sessionShow.getSessionShow01().getId());
        courseSyllabusContent.setSession(sessionShow01.getSession());
        courseSyllabusContent.setName(sessionShow01.getName());
        courseSyllabusContent.setTeachingHour(sessionShow01.getTeachingHour());
        courseSyllabusContent.setContent(sessionShow01.getContent());
        courseSyllabusContent.setImportant(sessionShow01.getImportant());
        courseSyllabusContent.setDifficult(sessionShow01.getDifficult());
        Integer updateCSCResult = courseSyllabusContentMapper.updateById(courseSyllabusContent);

        //返回结果
        return updateCSCResult;
    }

    @Override
    public Integer insertCourseSyllabusDescription(CourseSyllabusDescription courseSyllabusDescription) {
        return courseSyllabusDescriptionMapper.insert(courseSyllabusDescription);
    }
}
